# include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>,greater<int>> getTopKNum(vector<int> arr,int k) {
	vector<int> result;
	map<int,int> countNum;
	for(int i=0,size=arr.size(); i<size; ++i) {
		countNum[arr[i]]++;
	}
	priority_queue<int, vector<int>,greater<int>> minHeap;
	map<int,vector<int>> maxMap;
	for(const auto& pair:countNum) {
		int key=pair.first;
		int value=pair.second;
		if(minHeap.size()<k) {
			minHeap.push(key);
		} else {
			if(value>minHeap.top()) {
				minHeap.push(key);
			}
		}
	}
	while(!minHeap.empty()) {
		int key=minHeap.top();
		maxMap[countNum[key]].push_back(key);
		minHeap.pop();
	}
	priority_queue<int, vector<int>> maxHeap;
	for(const auto& pair:maxMap) {
		int key=pair.first;
		maxHeap.push(key);
	}
	while(!maxHeap.empty()) {
		vector<int> values=maxMap[maxHeap.top()];
		for(int i=0,size=values.size(); i<size; ++i) {
			cout<<values[i]<<" ";
		}
		maxHeap.pop();
	}
	return minHeap;
}
int main() {
	int k=2;
	vector<int> arr= {1, 2, 2, 1, 3, 1};
	priority_queue<int, vector<int>,greater<int>> result=getTopKNum(arr,k);
	return 0;
}
